package com.casic.modules.jy.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.casic.common.utils.R;
import com.casic.common.base.entity.PageEntity;
import com.github.pagehelper.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.casic.modules.jy.entity.JySjOptionEntity;
import com.casic.modules.jy.service.JySjOptionService;


/**
 * 试卷选项表
 * 
 * @author mfc
 * @email 123456789@qq.com
 * @date 2018-09-25 14:55:22
 */
@RestController
@RequestMapping("jysjoption")
public class JySjOptionController {
	@Autowired
	private JySjOptionService jySjOptionService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
        PageEntity<JySjOptionEntity> page=jySjOptionService.findPage(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") String id){
		JySjOptionEntity jySjOption = jySjOptionService.findById(id);
		return R.ok().put("jySjOption", jySjOption);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody JySjOptionEntity jySjOption) throws UnsupportedEncodingException {
		jySjOption.setOptionKey(URLDecoder.decode(jySjOption.getOptionKey(),"utf-8"));
		jySjOptionService.save(jySjOption);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody JySjOptionEntity jySjOption){
		jySjOptionService.edit(jySjOption);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] ids){
		jySjOptionService.removeByIds(ids);
		return R.ok();
	}

	@RequestMapping("/bathSave")
	public R bathSave(@RequestBody String jsonParamter){
		List<JySjOptionEntity> entityList = null;
		if(StringUtil.isNotEmpty(jsonParamter)){
			entityList = JSON.parseArray(jsonParamter,JySjOptionEntity.class);
		}else{
			return R.error("参数为空");
		}
		if(entityList != null && entityList.size()>0){
			for(JySjOptionEntity bean : entityList){
				jySjOptionService.save(bean);
			}
		}
		return R.ok();
	}



}
